#ifndef MSWEB_H
#define MSWEB_H
#ifdef __cplusplus
   extern "C"{
#endif
#include <libmsprotocol/msprotocol.h>

#define FILE_LOG_MSWEB			"/var/log/mscore/msweb.txt"
#define FILE_LOG_OPRECORD		"/var/log/mscore/opRecord.txt"

#define MSWEB_LOGIN_LOCKED_TIME			15	//多次失败错误锁定时间
#define MSWEB_LOGIN_FAILED_NUM			5	//失败次数后就锁定


/*服务引擎架构取名EV，致敬新能源车，致敬环保事业。
CP(connect pool)：连接池回调任务;
DCP(detect connect pool)：连接池的状态监测*/
//连接池数量模式
typedef enum MSEVTDCPType{			
	msev_tdcp_autocpu=0x00,	//和CPU数量保持一致；
	msev_tdcp_maxnum1,		//在设置值和CPU数量中取最大值；
	msev_tdcp_maxnum2,		//在设置值、CPU数量和msev_tdCp_numDefault中取最大值；
	msev_tdcp_custom,		//取设置的数值，和CPU无关；
}MSEVTDCPType;
typedef enum MSEVClientState{
	msev_state_unknow=0x00,
	msev_state_connecting,
	msev_state_connected,
	msev_state_in,
	msev_state_out,
	msev_state_close,
	msev_state_closed,
}MSEVClientState;
typedef enum MSEVConnectRet{
	msev_connect_ret_unknow=0x00,
	msev_connect_ret_longConnect,
	msev_connect_ret_close=(-0x01),
	msev_connect_ret_err=(-0x02),
}MSEVConnectRet;
typedef enum MSWEBLOGINRet{
	msweb_login_ret_nouser=0x01,
	msweb_login_ret_ok=0x00,
	msweb_login_ret_failed=(-0x01),
	msweb_login_ret_locked=(-0x02),
}MSWEBLOGINRet;
typedef struct MSWEBLOGINContext{
	ms_bool flag_init;
	ms_string name[2];
	ms_string pwd[2];
	MSLOCKContext mslock_ctt;
	ms_s08 errNum;		//错误次数
	ms_u64 lockedBaseTime;	//锁定时间
	ms_u64 lastErrBaseTime;	//锁定时间
	ms_byte exStr[256];	//错误额外信息
}MSWEBLOGINContext;

typedef struct MSEVAUTHENTContext{
	ms_bool flag_reqAuthent;	//启用请求鉴权
	ms_bool flag_web;			//网页还是接口鉴权
	ms_string pStrPrivatekey;	//配对私钥
	ms_string ignorPath;		//不鉴权的路径，格式PATH1，PATH2
	ms_u64 	timeoutSec;			//摘要超时时间
}MSEVAUTHENTContext;
typedef struct MSEVClientContext{
	ms_byte name[64];	//服务名称
	ms_byte ipaddr[32];
	ms_s32 fd;
	ms_u64 connectTime_us;	//记录通信的连接时间，用于计算请求耗时
	ms_u64 lastReqTime_us;	//记录上一次请求时间，用于超时丢弃连接
	MSEVClientState state;		
//在onConnect进行赋值	。
	ms_s32 cpu_index;
	ms_u32 event_opt;
	ms_string method;
	ms_string route;		
	MSEVAUTHENTContext *pEvAuthent_ctt;
}MSEVClientContext;

typedef struct MSEVIctContext{
	ms_byte ipaddr[128];
	ms_byte des[128];
	ms_u64 numTotal;
	ms_u64 numIct;
	ms_u64 lastReqTime;
	ms_u64 numper;
	ms_u64 baseTime;
	ms_u64 baseNum;
	struct MSEVIctContext *next;	
}MSEVIctContext;

typedef struct MSEVDCPContext{
	ms_bool flag_stop;
	ms_byte name[64];
	MSFIFOLISTContext * *ppFifoListCtt_cp;
	MSFIFOLISTContext * *ppFifoListCtt_dcp;
	MSLOCKContext *pLockCtt_cp;
	MSLOCKContext *pLockCtt_dcp;
	ms_void *pEv_ctt;
	int dcpSize;
	int max_event;
	int efd;
}MSEVDCPContext;
typedef struct MSEVCPContext{
	ms_bool flag_stop;
	ms_byte name[64];
	MSFIFOLISTContext * *ppFifoListCtt_cp;
	MSFIFOLISTContext * *ppFifoListCtt_dcp;
	MSLOCKContext *pMslockCtt_cp;
	MSLOCKContext *pMslockCtt_dcp;
	URLProtocol *purl_ptl;
	URLContext * purl_ctt;
	ms_void *pEv_ctt;			
	ms_u32 cp_index;
	ms_bool flag_longConnect;	
	int msepoll_fd;
}MSEVCPContext;
typedef struct MSEVContext{
/*------------基本属性------------*/	
	ms_string name;	//服务名称
	ms_s32 port;	//服务端口
	MSEVTDCPType tdCpType;	//cp的线程数量获取类型
	ms_s32 tdCpNum;	//cp的线程数量
/*------------接口回调------------*/	
	ms_void * cbArgs;	//回调函数参数
	MSEVConnectRet (*onConnect)(void *cbArgs,MSEVClientContext *);   //连接回调处理函数
	ms_bool (*onStop)(void *cbArgs);//指令函数:ms_false表示停止服务
	/*onConnect返回值大于0时，表示是长连接，可以使用onRecv、onSend和onClose回调*/
	ms_s08 (*onRecv)(void *cbArgs,MSEVClientContext *);	//客户端可读回调
	ms_s08 (*onSend)(void *cbArgs,MSEVClientContext *);	//客户端可写回调
	ms_s08 (*onClose)(void *cbArgs,MSEVClientContext *);	//客户端关闭回调
/*------------功能属性------------*/	
//拦截功能:ict=intercept
	ms_bool flag_ictAccess;		//启用拦截：访问
	ms_bool flag_ictResAddr;	//启用拦截：保留地址
	ms_s32 ictAccessTime_us;	//访问拦截间隔时间,单位微妙：
	MSEVIctContext *pIct_ctt;	//已拦截客户端信息
//检测连接池（detection connection pool，cdp）
	ms_u32 dcpSize;			//检测连接池存放大小
//启用禁用服务功能
	ms_bool *pflag_disable;	//关闭访问
//应用初始化完成标记
	ms_bool *pflag_appInit;	//某些应用需要外部初始化后，再对外提供服务
//服务协议相关参数，如果外部有需要用到，则传入即可	
	URLVal * purlval;
/*------------支持的请求方法------------*/	
	ms_string method;		//不支持的请求方法将被拦截
	ms_string route;		//只支持的路由，攻击拦截
/*------------请求鉴权------------*/	
	MSEVAUTHENTContext evAuthent_ctt;
/*------------服务对外------------*/
//连接池（Connection pool=cp）
	MSEVCPContext * ms_out pCp_ctt;
}MSEVContext;

#define msev_tdCp_numMax 		512
#define msev_tdCp_numDefault 	16
#define msev_timeout_dropReq	8	//DCP线程监听建立连接后，超过8秒没有发送请求则丢掉连接;

typedef enum MATCHSTREAM_RET{
	machstream_ret_maybePush			=-0x0001,
	machstream_ret_error				=-0x0002,
	machstream_ret_unknow				=-0x0101,
	machstream_ret_Forbidden			=-0x0102,
	machstream_ret_Unauthorizedaccess	=-0x0103,
	machstream_ret_nameerror			=-0x0104,
	machstream_ret_AssignChannel_err	=-0x0201,
}MATCHSTREAM_RET;

typedef enum MSWEBUSERTYpe{
	msweb_usertype_nor=0x00,
	msweb_usertype_inner,
	msweb_usertype_dev,
}MSWEBUSERTYpe;

#ifndef AH_WEB
#define AH_WEB		"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
#endif
#ifndef SPACE_WEB
#define SPACE_WEB	"&nbsp;&nbsp;&nbsp;&nbsp;"
#endif

#ifndef MSWEB_C
extern ms_void 
	msweb_api_headTitle(char *outbuf,char *title,char *page);
extern ms_void 
	msweb_api_cmHeadTitle(char *outbuf,char *title,char *page,WEBHOSTInfo webhost_info);
extern ms_void 
	msweb_api_tailTitle(char *outbuf, WEBHOSTInfo webhost_infos,ms_string version,ms_string mail_list);
extern ms_void 
	msweb_api_ok(char *outbuf);
extern ms_bool 
	msweb_api_ok_data(ms_string strBasekey,char *jsondata,char *outbuf);
extern void 
	msweb_api_unauthorizedAccess(char *outbuf);
extern ms_void 
	msweb_api_frequentVisits(char *outbuf,ms_s32 intercepttime);
extern ms_void 
	msweb_api_initFirst(char *outbuf);
extern ms_void 
	msweb_api_disableOut(char *outbuf);
extern ms_s32 
	msweb_api_charConvert(ms_string ms_in instr,ms_string ms_out outstr,ms_u32 ms_in len);
extern ms_void 
	msweb_api_formatConvert(char * inbuf,char *outbuf,int len);
extern ms_void 
	msweb_api_formatConvert2(char * inbuf,char *outbuf,int len);
extern ms_s32 
	msweb_api_convert2Char(ms_string ms_in instr,ms_string ms_out outstr,ms_u32 ms_in len);
extern ENUPROTOCOLIndex 
	msweb_api_getProtocol(WEBREQUESTContext *prequest_ctt);
extern MSWEBUSERTYpe 
	msweb_api_getUserType(ms_string userInfo);
extern ms_void 
	msweb_api_loginInit(MSWEBLOGINContext *pWLGCtt,ms_string user1,ms_string user2,ms_string pwd1,ms_string pwd2);
extern ms_void 
	msweb_api_loginDeinit(MSWEBLOGINContext *pWLGCtt);
extern MSWEBLOGINRet 
	msweb_api_login(MSWEBLOGINContext *pWLGCtt,ms_string outstr);
extern ms_void 
	msev_api_logfile(MSEVClientContext * pEvClient_ctt,const char *format,...);
extern ms_void 
	msev_api_logRecord(ms_byte * ipaddr,char *format,...);
extern ms_s32 
	msev_api_recv(MSEVClientContext * pmsdata,WEBREQUESTContext *prequest_ctt,ms_byte *pdatabuf,int len);
extern ms_void 
	msev_api_enpOutstr(ms_string  ms_out outstr,ms_string key,ms_string exter_str);
extern ms_string 
	msev_api_getReqInfo(MSEVContext *pmsev_ctt,ms_string desInfo,ms_string outStr);
extern ms_void 
	msev_api_createServer(MSEVContext *pserver_ctt);
#else
ms_void 
	msev_api_logfile(MSEVClientContext * pEvClient_ctt,const char *format,...);
#endif
#define msstr_intercept_black	"black"
#define msstr_intercept_white	"white"

#define MSWEB_FONT_STYLE 		"style=\"color:black;\""
#define MSWEB_H1_STYLE 			"style=\"color:red;\""
#define MSWEB_H2_STYLE 			"style=\"color:red;\""
#define MSWEB_HT1_STYLE 		"style=\"color:red;\""
#define MSWEB_HT2_STYLE 		"style=\"color:yellow;\""
#define MSWEBAPI_INTERCEPT_PERTIME		ms_usmseconds(500)

#define enp_exstring_nor 	"enc=1001&infocode=M2C1&usrinfo=NuoMi "	//糯米
#define enp_exstring_inner	"enc=2001&infocode=M3S2&usrinfo=InEr"	//银耳
#define enp_exstring_dev 	"enc=3001&infocode=M4D3&usrinfo=Cool"	//可乐

#ifdef __cplusplus
}
#endif
#endif
